package com.nari.ysphd.util;

import android.app.Activity;
import android.util.Log;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by sunxunchao on 2017/11/17.
 */

public class ActivityManager {

    /**
     * 维护Activity 的list
     */
    private static List<Activity> mActivates = Collections.synchronizedList(new LinkedList<Activity>());
    private static final String TAG = "ActivityManager";

    private static class SingletonHolder {
        private static ActivityManager instance = new ActivityManager();
    }

    /**
     * 私有的构造函数
     */
    private ActivityManager() {

    }

    public static ActivityManager getInstance() {
        return SingletonHolder.instance;
    }

    /**
     * @param activity 作用说明 ：添加一个activity到管理里
     */
    public void pushActivity(Activity activity) {
        mActivates.add(activity);
        Log.d(TAG, "activityList:size:" + mActivates.size());
    }

    /**
     * @param activity 作用说明 ：删除一个activity在管理里
     */
    public void popActivity(Activity activity) {
        mActivates.remove(activity);
        Log.d(TAG, "activityList:size:" + mActivates.size());
    }


    /**
     * get current Activity 获取当前Activity（栈中最后一个压入的）
     */
    public Activity currentActivity() {
        if (mActivates == null || mActivates.isEmpty()) {
            return null;
        }
        Activity activity = mActivates.get(mActivates.size() - 1);
        return activity;
    }

    /**
     * 结束当前Activity（栈中最后一个压入的）
     */
    public void finishCurrentActivity() {
        if (mActivates == null || mActivates.isEmpty()) {
            return;
        }
        Activity activity = mActivates.get(mActivates.size() - 1);
        finishActivity(activity);
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (mActivates == null || mActivates.isEmpty()) {
            return;
        }
        if (activity != null) {
            mActivates.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        if (mActivates == null || mActivates.isEmpty()) {
            return;
        }
        for (Activity activity : mActivates) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
            }
        }
    }

    /**
     * 按照指定类名找到activity
     *
     * @param cls
     * @return
     */
    public Activity findActivity(Class<?> cls) {
        Activity targetActivity = null;
        if (mActivates != null) {
            for (Activity activity : mActivates) {
                if (activity.getClass().equals(cls)) {
                    targetActivity = activity;
                    break;
                }
            }
        }
        return targetActivity;
    }

    /**
     * @return 作用说明 ：获取当前最顶部activity的实例
     */
    public Activity getTopActivity() {
        Activity mBaseActivity = null;
        synchronized (mActivates) {
            final int size = mActivates.size() - 1;
            if (size < 0) {
                return null;
            }
            mBaseActivity = mActivates.get(size);
        }
        return mBaseActivity;

    }

    /**
     * @return 作用说明 ：获取当前最顶部的acitivity 名字
     */
    public String getTopActivityName() {
        Activity mBaseActivity = null;
        synchronized (mActivates) {
            final int size = mActivates.size() - 1;
            if (size < 0) {
                return null;
            }
            mBaseActivity = mActivates.get(size);
        }
        return mBaseActivity.getClass().getName();
    }

    /**
     * 退出应用程序
     */
    public void appExit() {
        try {
            Log.e(TAG, "app exit");
            finishAllActivity();
        } catch (Exception e) {
        }
    }

    public List<Activity> getActivates(){
        return mActivates;
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        if (mActivates == null) {
            return;
        }
        for (Activity activity : mActivates) {
            activity.finish();
        }
        mActivates.clear();
    }

    public void finishActivityExceptCurrent(){
        Activity currentActivity = getTopActivity();
        if (mActivates == null) return;
        mActivates.remove(currentActivity);
        for (Activity activity : mActivates) {
            activity.finish();
        }
        mActivates.clear();
        mActivates.add(currentActivity);
    }

    public void finishActivityExcept(Class<?> cls){
        Activity currentActivity = findActivity(cls);
        if (mActivates == null) return;
        mActivates.remove(currentActivity);
        for (Activity activity : mActivates) {
            activity.finish();
        }
        mActivates.clear();
        mActivates.add(currentActivity);
    }
}
